package com.edz.from0507to0513apk.Base;

import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;

/**
 * author: Created by EDZ on 2018/5/8.
 * function:屏幕管理类
 */

public class ScreenManager {
    private static ScreenManager instance;

    public ScreenManager() {
    }

    public static ScreenManager getInstance() {
        synchronized (ScreenManager.class) {
            if (instance == null) {
                instance = new ScreenManager();
            }
        }
        return instance;
    }

    /**
     * 窗口全屏
     *
     * @param isChange
     * @param mActivity
     */
    public void setFullScreen(boolean isChange, BaseActivity mActivity) {
        if (!isChange) {
            return;
        }
        mActivity.getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mActivity.requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    /**
     * 沉浸状态栏
     *
     * @param isChange
     * @param mActivity
     */
    public void setStatusBar(boolean isChange, BaseActivity mActivity) {
        if (!isChange) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 透明状态栏
            mActivity.getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 透明导航栏
            mActivity.getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    /**
     * 旋转屏幕
     *
     * @param isChange
     * @param mActivity
     */
    public void setScreenRoate(boolean isChange, BaseActivity mActivity) {
        if (!isChange) {
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

    /**
     * 获取Android设备状态栏高度
     *
     * @param baseApplication
     * @return
     */
    public int getStatusBarHeight(BaseApplication baseApplication) {
        int height = 0;
        int resourceId = baseApplication.getBaseContext().getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            height = baseApplication.getBaseContext().getResources().getDimensionPixelSize(resourceId);
        }
        return height;
    }

    /**
     * 获取Android屏幕宽高
     *
     * @param mActivity
     * @return
     */
    public int getWidthAndHeight(BaseActivity mActivity) {
        Resources resources = mActivity.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        return width + height;
    }
}
